package com.clstu.stack;

import java.util.Scanner;

/**
 * 用Array实现栈结构的案例
 */
public class ArrayStackDemo {
    public static void main(String[] args) {
        //测试是否写对
        //创建栈
        ArrayStack arrayStack = new ArrayStack(4);
        //用菜单
        String key = "";//接受游湖输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("\n======测试栈==========");
            System.out.println("  push: 入栈");
            System.out.println("  pop:  出栈");
            System.out.println("  look: 查看栈");
            System.out.println("  peek: 看栈顶");
            System.out.println("  exit: 退出程序");
            System.out.print("  请输入:");
            key = scanner.next();
            switch (key){
                case "push":
                    System.out.print("请输入要入栈的数:");
                    arrayStack.push(scanner.nextInt());
                    break;
                case "pop":
                    try {
                        System.out.println("出栈:"+arrayStack.pop());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "look":
                    arrayStack.look();
                    break;
                case "peek":
                    try {
                        System.out.println("栈顶:"+arrayStack.peek());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case "exit":
                    loop =false;
                    break;
            }
        }
        System.out.println("程序结束~~~");
    }
}

/**
 * 一个基于数组的栈类
 */
class ArrayStack{
    private int maxSize;//最大占空间
    private int[] arr;//数组,用于保存数据
    private int top;//指向栈顶,-1表示空

    //构造器
    public ArrayStack(int maxSize){
        this.maxSize = maxSize;
        arr = new int[maxSize];
        top = -1;
    }

    //是否栈空
    public boolean isEmpty(){
        return top==-1;
    }

    //是否栈满
    public boolean isFull(){
        return top == maxSize-1;
    }

    //压栈
    public void push(int data){
        //先判断是否栈满
        if(isFull()){
            System.out.println("栈满,压栈失败");
            return;
        }
        //压栈
        top++;
        arr[top] = data;
    }

    //出栈
    public int pop(){
        //先判断是否栈空
        if(isEmpty()){
            throw new RuntimeException("栈空,出栈失败");//抛出异常就会退出方法,无需再return,
        }
        //出栈
        int val = arr[top];
        top--;
        return val;
    }

    //编写方法显示栈中的所有数据
    public void look(){
        //如果栈空
        if(isEmpty()){
            System.out.println("栈空,没有数据");
            return;
        }
        //遍历打印
        for (int i = top; i >= 0; i--) {
            System.out.printf("arr[%d] = %d \n",i,arr[i]);
        }
    }

    //编写方法查看栈顶数据,只查看,不出栈
    public int peek(){
        //是否栈空
        if(isEmpty()){
            throw new RuntimeException("栈空,查看栈顶失败");
        }
        return arr[top];
    }
}